// pic.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define width 840
#define height 650

int rows,cols;
int gridWidth,gridHeight;
int allHeight,allWidth;
int c;
SYSTEMTIME startTime;
int sum;
int t1,t2;

IMAGE origin;
IMAGE a[9];
IMAGE clockImages[10];
IMAGE dot;
IMAGE img;

void initdata();
void show();
void loadAllImage();
void putImage();
void playTime();
void mousec();
void gameOver();

void main(){
	initgraph(width,height);
	setbkcolor(WHITE);
	GetLocalTime(&startTime);
	initdata();
	cleardevice();
	loadAllImage();
	putImage();
	show();
	
	while(1){
	    playTime();
		gameOver();
		mousec();
	} 
	_getch();
	closegraph();
}

void initdata(){
	rows=3;cols=3;
	gridWidth=100;gridHeight=100;
	allHeight=3*gridHeight;allWidth=3*gridWidth;
	c=0;
}
void show(){
	for(int i=0;i<cols+1;i++){
		setlinecolor(RGB(0,0,0));
		line(gridWidth*i,0,gridWidth*i,allHeight);
		line(gridWidth*i+10*i-4,allHeight+40,gridWidth*i+10*i-4,height);
		for(int j=0;j<rows+1;j++){
			line(0,gridHeight*i,allWidth,gridHeight*i);
			line(0,gridHeight*i+allHeight+40,allWidth+35,gridHeight*i+allHeight+40);
		}
	}
}
void loadAllImage(){
	loadimage(&origin,_T("./images/1.jpg"));
    loadimage(&a[0], _T("./images/1_01.jpg"));
	loadimage(&a[1], _T("./images/1_02.jpg"));
	loadimage(&a[2], _T("./images/1_03.jpg"));
	loadimage(&a[3], _T("./images/1_04.jpg"));
	loadimage(&a[4], _T("./images/1_05.jpg"));
	loadimage(&a[5], _T("./images/1_06.jpg"));
	loadimage(&a[6], _T("./images/1_07.jpg"));
	loadimage(&a[7], _T("./images/1_08.jpg"));
	loadimage(&a[8], _T("./images/1_09.jpg"));
	loadimage(&origin, _T("./images/1.jpg"));
	loadimage(&clockImages[0],_T("./images/t0.bmp"));
	loadimage(&clockImages[1],_T("./images/t1.bmp"));
	loadimage(&clockImages[2],_T("./images/t2.bmp"));
	loadimage(&clockImages[3],_T("./images/t3.bmp"));
	loadimage(&clockImages[4],_T("./images/t4.bmp"));
	loadimage(&clockImages[5],_T("./images/t5.bmp"));
	loadimage(&clockImages[6],_T("./images/t6.bmp"));
	loadimage(&clockImages[7],_T("./images/t7.bmp"));
	loadimage(&clockImages[8],_T("./images/t8.bmp"));
	loadimage(&clockImages[9],_T("./images/t9.bmp"));
	loadimage(&dot,_T("./images/dot.bmp"));
	
}
void putImage(){
	putimage(480,0,&origin);
	for(int i=0;i<3;i++){
		for( int j=0;j<3;j++){
			putimage(gridWidth*i+10*i,gridHeight*j+allHeight+40,&a[8-c]);
			c++;
		}
	}
	putimage(624,390,&dot);
	putimage(624,410,&dot);
}

void playTime(){
	SYSTEMTIME cTime;
	GetLocalTime(&cTime);
	sum=0;
	sum+=cTime.wHour-startTime.wHour;
	sum*=60;
	sum+=cTime.wMinute-startTime.wMinute;
	sum*=60;
	sum+=cTime.wSecond-startTime.wSecond;

	int i=1,j=1;
	t1=sum;
	t2=sum/60;
	int a,b;
	while(i!=-1){
		if(i==1){
			t1=sum%60;
		}
		a=t1%10;
		putimage(650+25*i,380,&clockImages[a]);
		t1/=10;
		i--;
	}
	while(j!=-1){
		b=t2%10;
		putimage(550+25*j,380,&clockImages[b]);
		t2/=10;
		j--;
	}
}

void mousec(){
	MOUSEMSG m;
		if(MouseHit()){
			m=GetMouseMsg();
			if(m.mkLButton){
				if(m.y>(allHeight+40)){
					for(int i=0;i<3;i++){
						for(int j=0;j<3;j++){
						    if(m.x>(gridWidth+10)*i&&m.x<(i+1)*(gridWidth+10)&&
						       m.y>(allHeight+40+gridHeight*j)&&m.y<(allHeight+40+gridHeight*(j+1))){
							    getimage(&img,(gridWidth+10)*i,(allHeight+40+gridHeight*j),100,56);
							}
						}
					}
				}
				if(m.y<allHeight){
					for(int i=0;i<3;i++){
						for(int j=0;j<3;j++){
			                if(m.x>gridWidth*i&&m.x<gridWidth*(i+1)&&
			    	           m.y>gridHeight*j&&m.y<gridHeight*(j+1)){
				                putimage(gridWidth*i,gridHeight*j,&img);
							}
						}
					}
				}
		   }
		}
}
void gameOver(){
	SYSTEMTIME cTime;
	GetLocalTime(&cTime);
	sum=0;
	sum+=cTime.wHour-startTime.wHour;
	sum*=60;
	sum+=cTime.wMinute-startTime.wMinute;
	sum*=60;
	sum+=cTime.wSecond-startTime.wSecond;
	t1=sum;
	t2=sum/60;
	while(t2>=1){	
     outtextxy(width/2-10,height/2-10,"游戏结束！");
    break;
	}
}
